<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/04/18
 */
namespace common\models;

use yii;

/**
 * Class Booker   id 双向映射
 * @package common\models
 */
class Booker extends Base {
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'dive_booker_map';
    }

    const TYPE_BOAT = 'boat';
    const TYPE_TRIP = 'trip';
    const TYPE_TOUR = 'tour';
    const TYPE_CABIN = 'cabin';
    const TYPE_OCCUPANCY = 'occupancy';
    const TYPE_PRODUCT = 'product';
    const TYPE_BOAT_PRODUCT='boat_product';

    const SYNC_404 = 'NOT FOUND';
    const SYNC_DELETED = 'DELETED';

    public static function dive2booker($id, $type) {
        return self::getScalar("dive_id=$id AND `type`='$type'", 'booker_id');
    }

    public static function booker2dive($id, $type) {
        return self::getScalar("booker_id=$id AND `type`='$type'", 'dive_id');
    }

    public static function add($diveId, $bookerId, $type) {
        $sql = "REPLACE INTO " . self::tableName() . " (`dive_id`,`booker_id`,`type`) VALUES ($diveId,$bookerId,'$type')";
        Yii::$app->db->createCommand($sql)->execute();
    }

    public static function exists($diveId, $bookerId, $type) {
        return (bool)self::getScalar("dive_id=$diveId AND booker_id=$bookerId AND type='$type'");
    }

    public static function delAll($type) {
        self::delete("`type`=$type");
    }
}
